package com.hb.common.handler.backup;

import cn.hutool.core.util.StrUtil;
import com.sl.ecm.backup.constant.Config;
import com.sl.ecm.backup.constant.DbCommand;
import com.sl.ecm.backup.dto.Database;
import com.sl.ecm.backup.enums.DbEnum;
import com.sl.ecm.backup.factory.DbBackupFactory;
import com.sl.ecm.backup.handler.os.OSHandler;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * postgresql备份处理程序
 */
@Component
@Lazy(false)
public class PostgresqlBackupHandler extends AbstractDbBackupHandler {

	@Override
	protected void backupAfter(String dbBackupPath) {

	}

	@Override
	protected void restoreAfter(String dbProgramPath) {

	}

    /**
     * 创建postgresql备份命令
     *
     * @param database   数据源信息
     * @param dbFilePath 备份文件路径
     * @return 备份命令字符串
     */
    @Override
    protected String[] createBackupCommand(Database database, String dbFilePath) {
        String command = StrUtil.format(DbCommand.POSTGRESQL_BACKUP,
                database.getHost(),
                database.getPort(),
                database.getUsername(),
                database.getDbName(),
                dbFilePath);

        // 组装命令
        return assembleCommand(database, command);
    }

    /**
     * 创建postgresql还原命令
     *
     * @param database   数据源信息
     * @param dbFilePath 还原文件路径
     * @return 还原命令字符串
     */
    @Override
    protected String[] createRestoreCommand(Database database, String dbFilePath) {
        String command = StrUtil.format(DbCommand.POSTGRESQL_RESTORE,
                database.getHost(),
                database.getPort(),
                database.getUsername(),
                database.getDbName(),
                dbFilePath);

        // 组装命令
        return assembleCommand(database, command);
    }

    /**
     * 组装命令
     *
     * @param database 数据源信息
     * @param command  命令字符
     */
    private String[] assembleCommand(Database database, String command) {
        OSHandler osHandler = osUtil.getOSHandler();
        String[] cmd = osHandler.assembleCommand(command);

        if (osHandler.getOSName().equals(Config.WINDOWS)) {
            cmd[2] = "set PGPASSWORD=" + database.getPassword() + "&&" + cmd[2];
        } else {
            cmd[2] = "export LD_LIBRARY_PATH=" + dbProgramPath() + "/libpq.so.5:$LD_LIBRARY_PATH" +
                    " && export PGPASSWORD=" + database.getPassword() +
                    " && " + cmd[2];
        }
        return cmd;
    }

    /**
     * 获取postgresql数据库程序路径
     *
     * @return postgresql数据库程序路径
     */
    @Override
    protected String dbProgramPath() {
        OSHandler osHandler = osUtil.getOSHandler();
        return pathPermissions.getDbProgramPath() + osHandler.getOsPath() + DbEnum.POSTGRESQL.name().toLowerCase() + "/bin";
    }

    /**
     * 注册postgresql备份处理程序
     */
    @Override
    public void afterPropertiesSet() {
        DbBackupFactory.register(DbEnum.POSTGRESQL, this);
    }

    @Override
    public String dbFileTye() {
        return "sql";
    }
}

